package com.wwfish.cmsui.modules.channel.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ContextMenuEvent;
import com.google.gwt.event.dom.client.ContextMenuHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.nexustar.gwt.widgets.client.asyn.FishAsyncCallback;
import com.nexustar.gwt.widgets.client.asyn.IAsyncModelCallback;
import com.nexustar.gwt.widgets.client.ui.iprovider.ITreeProvider;
import com.nexustar.gwt.widgets.client.ui.panel.ContentPanel;
import com.nexustar.gwt.widgets.client.ui.panel.InfoPanel;
import com.nexustar.gwt.widgets.client.ui.toolbar.MyMenuBar;
import com.nexustar.gwt.widgets.client.ui.toolbar.MyMenuItem;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolBar;
import com.nexustar.gwt.widgets.client.ui.toolbar.ToolItem;
import com.nexustar.gwt.widgets.client.ui.tree.ViewTree;
import com.nexustar.gwt.widgets.client.ui.window.IMessageConfirmCall;
import com.nexustar.gwt.widgets.client.ui.window.Message;
import com.wwfish.cms.model.ChannelDto;
import com.wwfish.cms.service.ChannelManager;
import com.wwfish.cmsui.modules.common.client.util.ServiceFactory;
import com.wwfish.gwt.reflection.client.ReflectionHelper;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2010-8-6
 * Time: 14:49:45
 * To change this template use File | Settings | File Templates.
 */
public class SequenceTreeWidget extends Composite {
    private ViewTree sequenceTree;
    private MyMenuBar treeContextMenu;
    private PopupPanel contextMenuPanel;
    private ToolBar bar;

    private IAsyncModelCallback saveFinishModelCallback;


    public ViewTree getTree() {
        return sequenceTree;
    }

    public SequenceTreeWidget(IAsyncModelCallback saveFinishModelCallback) {
        this.saveFinishModelCallback = saveFinishModelCallback;
        ContentPanel wrapper = new ContentPanel("栏目排序");
        wrapper.setWidth("100%");
        initSequenceTree();
        initBar();
        VerticalPanel vp = new VerticalPanel();
        vp.setWidth("100%");
        vp.add(bar);
        vp.add(sequenceTree);
        wrapper.addContentWidget(vp);
        initWidget(wrapper);
    }

    public void initSequenceTree() {
        sequenceTree = new ViewTree(new ITreeProvider() {
            public String getTreeItemText(Object entity) {
                ChannelDto channel = (ChannelDto) entity;
                return channel.getName();
            }

            public Image getTreeItemIcon(Object entity) {
                return null;
            }

            public boolean hasChildren(Object entity) {
                return false;
            }

            public void getChildren(Object parentEntity, IAsyncModelCallback asyn) {

            }

            public void setInputData(Object[] paramters, IAsyncModelCallback asyn) {

            }
        });
        initTreeContextMenu();
        sequenceTree.addContextMenuHandler(new ContextMenuHandler() {
            public void onContextMenu(ContextMenuEvent contextMenuEvent) {
                contextMenuPanel.setPopupPosition(contextMenuEvent.getNativeEvent().getClientX() + 3,
                        contextMenuEvent.getNativeEvent().getClientY());
                contextMenuPanel.show();
            }
        });
    }

    private void initBar() {
        bar = new ToolBar();
        bar.setWidth("100%");
        ToolItem save = new ToolItem("确认保存");
        bar.addToolItem(save);

        save.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                List result = (List) sequenceTree.getModelManger().getModel();
                ServiceFactory.invoke(ChannelManager.class.getName(), "sequenceChannel", new Object[]{result}, new FishAsyncCallback() {
                    public void onSuccess(Object o) {
                        InfoPanel.show("排序成功！");
                        saveFinishModelCallback.setModelElments(o);
                    }
                });
            }
        });


    }

    private void initTreeContextMenu() {

        treeContextMenu = new MyMenuBar(true);
//        treeContextMenu.setWidth("160px");


        MyMenuItem up = new MyMenuItem("上移", new Command() {
            public void execute() {
                sequenceTree.upSelectedItem();
            }
        });
        MyMenuItem down = new MyMenuItem("下移", new Command() {
            public void execute() {
                sequenceTree.downSelectedItem();
            }
        });


        treeContextMenu.addItem(up);
        treeContextMenu.addItem(down);

        contextMenuPanel = new PopupPanel(true);
        contextMenuPanel.setWidget(treeContextMenu);
    }


}
